package com.elite.groovy.callback

import com.alibaba.fastjson.JSONObject
import com.elite.groovy.kh.KHDBConfig
import com.elite.groovy.kh.KHUtil
import com.elite.groovy.util.EliteBean
import com.elite.groovy.util.IpAddressUtil
import com.wisdge.dataservice.Result
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
/**
 * @projectName: ngsapitask-v3
 * @package: cn.com
 * @className: chatproxy
 * @author: LiJT
 * @description: 网聊机器人模拟Token
 * @date: 2022-09-29 13:21
 * @version: 1.0
 */

public class apiChatSoBotToken implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiChatSoBotToken.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        long startTime = System.currentTimeMillis();
        String outData = "";

        // 调用IP过滤
        if (KHDBConfig.getParam(eliteBean, "#APIIP") == "1" && !KHUtil.getRemoteHost(request)) {
            outData = "API被非法IP调用-Request Not Allow!" + IpAddressUtil.getIpAddress(request);
            log.warn(outData);
            return JSONObject.toJSONString(new Result(Result.ERROR, outData));
        }

        String getMethod = request.getMethod();
        log.debug("HTTP请求调用类型 getMethod:" + getMethod);
        if (getMethod != "POST" && getMethod != "GET") {
            outData = "HTTP请求调用类型错误，接口调用失败：" + getMethod;
            log.error(outData);
            return JSONObject.toJSONString(new Result(Result.ERROR, outData));
        }

        // 获取所有Header
        Map<String, String> mapHeader = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            log.debug("Call Header-" + key + ":" + value);

            mapHeader.put(key, value);
        }

        // 获取所有参数名
        Map<String, String> mapParam = new HashMap<String, String>();
        Enumeration ep = request.getParameterNames();
        while (ep.hasMoreElements()) {
            String key = (String) ep.nextElement();
            String value = request.getParameter(key);
            log.debug("Call Parameter-" + key + ":" + value);
            mapParam.put(key, value);

        }

        // 获取数据源List<Map<String, Object>>
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

        try {
            // 模拟Token
            JSONObject tokenJson = new JSONObject();
            tokenJson.put("access_token", "access_token");

            // 处理Token信息
            outData = JSONObject.toJSONString(new Result(Result.SUCCESS, "处理Token完成" + " 总耗时:" + (System.currentTimeMillis() - startTime).toString(), tokenJson));

            log.debug("处理Token完成【返回结果】：" + outData + " 总耗时:" + (System.currentTimeMillis() - startTime).toString());

            return outData;

        } catch (Exception e) {
            outData = "HTTP请求调用错误，接口处理异常：" + e.getMessage();
            log.error(outData, e);
            return JSONObject.toJSONString(new Result(Result.ERROR, outData));

        }
    }
}
